﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using KerriganDirection.Helpers;
using System.Diagnostics;
using KerriganDirection.DirectionServiceWCF;
using System.ComponentModel;

namespace KerriganDirection.SessionMgt
{
    /// <summary>
    /// Interaction logic for SessionsList.xaml
    /// </summary>
    public partial class ucSessionsList : UserControl, INotifyPropertyChanged
    {
        #region NotifyPropertyChanged

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        #endregion

        #region Properties

        private MainModule _MainModuleVO;
        public MainModule MainModuleVO
        {
            get
            {
                return this._MainModuleVO;
            }
            set
            {
                this._MainModuleVO = value;
                OnPropertyChanged("MainWindowVO");
            }
        }

        #endregion

        #region Methods

        public ucSessionsList()
        {
            InitializeComponent();

            this.MainModuleVO = (App.Current.Windows[0] as MainWindow).MainModuleVO;

            RefreshList();
        }

        public void RefreshList()
        {
            try
            {
                this.SessionListDataGrid.ItemsSource = null;
                this.MainModuleVO.SessionModuleVO.GetListSessionToDisplay();
                this.SessionListDataGrid.ItemsSource = this.MainModuleVO.SessionModuleVO.ListSessionsVOToDisplay;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        private void bttDelete_Click(object sender, RoutedEventArgs e)
        {
            int CurrentSessionId = (int)((Button)sender).CommandParameter;

            try
            {
                this.MainModuleVO.SessionModuleVO.DeleteSession(CurrentSessionId);
                this.SessionListDataGrid.ItemsSource = null;
                this.MainModuleVO.SessionModuleVO.GetListSessionToDisplay();
                this.SessionListDataGrid.ItemsSource = this.MainModuleVO.SessionModuleVO.ListSessionsVOToDisplay;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }

        #endregion
    }
}
